//
//  HYSettingViewController.m
//  HaiYue
//
//  Created by skyline on 2018/1/19.
//  Copyright © 2018年 skyline. All rights reserved.
//

#import "HYSettingViewController.h"
#import "HYSettingCell.h"
@interface HYSettingViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *imageArr;
@property (nonatomic, strong) NSArray *titleArr;
@end

@implementation HYSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"设置中心";
    self.imageArr = @[@"helpcenter_send_message",@"helpcenter_automatic_install",@"helpcenter_delete_installpackage",
                      @"helpcenter_update",@"download_installed_more",@"helpcenter_clear_normal"];
    self.titleArr = @[@"消息推送",@"下载后自动安装",@"安装后自动删除安装包",@"检查更新",@"意见反馈",@"清除缓存"];
    // Do any additional setup after loading the view.
}

- (UITableView *)tableView
{
    if (!_tableView)
    {
        _tableView = [[UITableView alloc] init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.bounces = NO;
        _tableView.tableFooterView = [[UIView alloc]init];
        _tableView.separatorColor = CELL_LINE_COLOR;
        [self.view addSubview:_tableView];
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_offset(UIEdgeInsetsZero);
        }];
    }
    return _tableView;
}

#pragma mark - UITableViewDataSource method
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 6;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * identifier = @"SettingCellID";
    HYSettingCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell)
    {
        cell = [[HYSettingCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.imageName = self.imageArr[indexPath.row];
    cell.title = self.titleArr[indexPath.row];
    cell.contentLBL.hidden = (indexPath.row == 3)?NO:YES;
    cell.contentLBL.text = @"v1.0.0";
    if (indexPath.row == 0)
    {
        cell.switchBtn.on = ([HYSettingManager manager].messageOn)?YES:NO;
    }
    else if (indexPath.row == 1)
    {
        cell.switchBtn.on = ([HYSettingManager manager].installOn)?YES:NO;
    }
    else if (indexPath.row == 2)
    {
        cell.switchBtn.on = ([HYSettingManager manager].deleteOn)?YES:NO;
    }
    return cell;
}
#pragma mark - UITableViewDelegate method
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return HYSettingCellHeight;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    HYSettingCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (indexPath.row < 3)
    {
        cell.switchBtn.on = !cell.switchBtn.on;
        if (indexPath.row == 0)
        {
            [HYSettingManager manager].messageOn = cell.switchBtn.on;
        }
        else if (indexPath.row == 1)
        {
            [HYSettingManager manager].installOn = cell.switchBtn.on;
        }
        else if (indexPath.row == 2)
        {
            [HYSettingManager manager].deleteOn = cell.switchBtn.on;
        }
    }
    else if (indexPath.row == 3)
    {
        
    }
    else if (indexPath.row == 4)
    {
        [UIView animateWithDuration:0 delay:0.5 options:UIViewAnimationOptionLayoutSubviews animations:^{} completion:^(BOOL finished) {
            id vc = [NSClassFromString(@"HYFeedBackViewController") new];
            [vc setValue:@"提交反馈" forKeyPath:@"buttonTitle"];
            [self.navigationController pushViewController:vc animated:YES];
        }];
    }
    else
    {
        cell.imageName = ([cell.imageName isEqualToString:@"1"])?@"4":@"1";
    }
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.style = HYViewNaviBarStyleOnlyLeftBtn;
    self.tableView.hidden = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
